package com.cyf.db.mapper;

import com.cyf.app.network.bean.AppBusinessReq;
import com.cyf.db.bean.UsersWechat;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * 用户和微信公众号的对应关系
 *
 * @author hfy
 * @date 2018/7/27
 */
public interface UsersWechatMapper {

    @Select("select * from users_wechat where userId = #{userId} and parkingId = #{parkingId} limit 0, 1")
    UsersWechat getByUserIdAndParkingId(@Param("userId") Integer userId, @Param("parkingId") Integer parkingId);

    @Select("select openid from users_wechat where userId = #{userId} and apiUserId = #{apiUserId} limit 0, 1")
    String getOpenidByUserIdAndApiUserId(@Param("userId") Integer userId, @Param("apiUserId") Integer apiUserId);

    @Select("select openid,apiUserId from users_wechat where userId = #{userId}")
    List<UsersWechat> listByUserId(@Param("userId")Integer userId);

    @Select("select * from users_wechat where openid = #{openid}  limit 0, 1")
    UsersWechat getByOpenid(@Param("openid") String openid);

    @Select("select * from users_wechat where phone = #{phone} limit 0, 1")
    UsersWechat getByPhone(@Param("phone") String phone);

    @Select("select apiUserId from users_wechat where openid = #{openid} limit 0, 1")
    Integer getApiUserIdByOpenid(@Param("openid") String openid);

    @Insert("insert into users_wechat(apiUserId, userId, openid, parkingId, localPersonId, personName,headImgurl,phone,createTime) " +
            "values(#{u.apiUserId}, #{u.userId}, #{u.openid}, #{u.parkingId}, #{u.localPersonId}, #{u.personName},#{u.headImgurl},#{u.phone},#{u.createTime})")
    void insert(@Param("u") UsersWechat usersWechat);

    @Update(" UPDATE users_wechat SET phone =#{phone},createTime = #{createTime} WHERE id = #{id}  ")
    void updateUserWechat(@Param("phone") String phone,@Param("createTime") Date createTime,@Param("id") Integer id);


    @Update("update users_wechat set parkingId = #{parkingId}, localPersonId = #{localPersonId} where id = #{id}")
    void updateParkingIdAndPersonIdById(@Param("parkingId") Integer parkingId,
                                        @Param("localPersonId") Integer localPersonId,
                                        @Param("id") Integer id);

    @Update("update users_wechat set personName = #{personName} where id = #{id}")
    void updatePersonNameById(@Param("personName") String personName, @Param("id") Integer id);

    @Update("update users_wechat set parkingId = #{parkingId}, localPersonId = #{localPersonId}, " +
            "personName = #{personName} " +
            "where id = #{id}")
    void updateExtraInfoById(@Param("parkingId") Integer parkingId,
                             @Param("localPersonId") Integer localPersonId,
                             @Param("personName") String personName,
                             @Param("id") Integer id);

    @Select("select * from users_wechat where apiUserId=22 and userId=#{userId}")
    UsersWechat selectUsersWechat(@Param("userId") int userId);

    @Insert("insert into users_wechat (userId,apiUserId,openid) " +
            "values (#{usersWechat.userId},22,#{usersWechat.openid})")
    void insertUsersWechat(@Param("usersWechat") UsersWechat usersWechat);


    @Select("select * from users_wechat where userId = #{userId} limit 0, 1 ")
    UsersWechat selectlistByUserId(@Param("userId")Integer userId);

    @Select("<script>" +
            " select * from users_wechat " +
            "  <where>  " +
            "<if test=\"req.opendId != null and req.opendId != '' \">" +
            " AND openid = #{req.opendId} " +
            "</if>" +
            "<if test=\"req.phone != null and req.phone != '' \">" +
            " AND phone = #{req.phone} " +
            "</if>" +
            "  </where>  " +
            " limit 0, 1 " +
            "</script>")
    UsersWechat getByOpenidAndPhone(@Param("req")AppBusinessReq req);
}
